#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Author: kindey
@Date: 2025/8/7
@Description: 
"""
# apps/utils/response_utils.py
from django.http import JsonResponse
from rest_framework.response import Response
from rest_framework import status

def api_response(code, msg, data=None):
    """
    统一API响应格式
    :param code: 状态码
    :param msg: 消息
    :param data: 数据
    :return: JsonResponse
    """
    response_data = {
        'code': code,
        'msg': msg
    }

    if data is not None:
        response_data['data'] = data

    return JsonResponse(response_data)

# 成功响应的快捷方式
def success_response(data=None, msg='操作成功'):
    return api_response('200', msg, data)

# 错误响应的快捷方式
def error_response(msg='操作失败', code='500'):
    return api_response(code, msg)

def rest_api_rep(code, msg, data=None):
    """
    统一API响应格式
    :param code: 状态码
    :param msg: 错误消息
    :param data: 数据
    :return: Response
    """
    response_data = {
        'code': code,
        'msg': msg
    }

    if data is not None:
        response_data['data'] = data

    return Response(response_data, status=status.HTTP_200_OK)

def rest_api_success(data=None, msg='操作成功'):
    return rest_api_rep('200', msg, data)

def rest_api_error(msg='操作失败', code='500'):
    return rest_api_rep(code, msg)
